package com.rmy.queue.controller;


import com.rmy.common.core.domain.AjaxResult;
import com.rmy.common.core.page.TableDataInfo;
import com.rmy.common.core.text.Convert;
import com.rmy.queue.domain.QmsBusinessConfig;
import com.rmy.queue.domain.QmsMainBusiness;
import com.rmy.queue.service.IQmsMainBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主页Controller
 *
 * @author rmy
 * @date 2022-10-06
 */
@RestController
@RequestMapping("/queue/mainpage")
public class QmsMainPageController {

    @Autowired
    private IQmsMainBusinessService qmsMainBusinessService;

    /**
     * 查询业务配置列表
     */
//    @PreAuthorize("@ss.hasPermi('queue:mainpage:menu')")
    @GetMapping("/getSummaryData")
    public AjaxResult getSummaryData(@RequestParam(required = false) String orgNo, @RequestParam(required = false) String erpBusinessType)
    {
        QmsMainBusiness qmsMainBusiness = new QmsMainBusiness();
        qmsMainBusiness.setOrgNo(orgNo);
        qmsMainBusiness.setBusinessType(erpBusinessType);
        // 1.今日预约数量
        String bookingCount = qmsMainBusinessService.selectBookingCount(qmsMainBusiness);
        // 2.今日签到车辆
        String takenoCount = qmsMainBusinessService.selectTakeNoCount(qmsMainBusiness);
        // 3.即将到号车辆 取今天已经取号的个数
        String waitingCallCount = qmsMainBusinessService.selectWaitingCallCount(qmsMainBusiness);
        // 4.正在叫号的车辆
        String callingCount = qmsMainBusinessService.selectCallingCount(qmsMainBusiness);
        // 5.今日进场车辆
        String entryCount = qmsMainBusinessService.selectEntryCount(qmsMainBusiness);
        // 6.今日离场车辆
        String exitCount = qmsMainBusinessService.selectExitCount(qmsMainBusiness);

        Map<String, Object> map = new HashMap<>();
        map.put("bookingCount", Convert.toInt(bookingCount));
        map.put("takenoCount", Convert.toInt(takenoCount));
        map.put("waitingCallCount", Convert.toInt(waitingCallCount));
        map.put("callingCount", Convert.toInt(callingCount));
        map.put("entryCount", Convert.toInt(entryCount));
        map.put("exitCount", Convert.toInt(exitCount));
        return AjaxResult.success(map);
    }

    /**
     * 查询业务配置列表
     */
//    @PreAuthorize("@ss.hasPermi('queue:mainpage:menu')")
    @GetMapping("/getChartsData")
    public AjaxResult getChartsData(@RequestParam(required = false) String orgNo, @RequestParam(required = false) String erpBusinessType)
    {
        Map<String, Object> map = new HashMap<>();
        QmsMainBusiness qmsMainBusiness = new QmsMainBusiness();
        qmsMainBusiness.setOrgNo(orgNo);
        qmsMainBusiness.setBusinessType(erpBusinessType);
        List<Map<String, Object>> bookingList = qmsMainBusinessService.selectWeekBookingCount(qmsMainBusiness);
        List<Integer> mapBookingList = new ArrayList<>();
        List<String> xDateList = new ArrayList<>();
        for (Map<String, Object> weekOnline: bookingList) {
            xDateList.add(Convert.toStr(weekOnline.get("x_date"), ""));
            mapBookingList.add(Convert.toInt(weekOnline.get("booking_num"), 0));
        }
        List<Map<String, Object>> takeNoList = qmsMainBusinessService.selectWeekTakeNoCount(qmsMainBusiness);
        List<Integer> mapTakeNoList = new ArrayList<>();
        for (Map<String, Object> weekOnline: takeNoList) {
            mapTakeNoList.add(Convert.toInt(weekOnline.get("takeno_num"), 0));
        }
        List<Map<String, Object>> entryList = qmsMainBusinessService.selectWeekEntryCount(qmsMainBusiness);
        List<Integer> mapEntryList = new ArrayList<>();
        for (Map<String, Object> weekOnline: entryList) {
            mapEntryList.add(Convert.toInt(weekOnline.get("entry_num"), 0));
        }
        List<Map<String, Object>> exitList = qmsMainBusinessService.selectWeekExitCount(qmsMainBusiness);
        List<Integer> mapExitList = new ArrayList<>();
        for (Map<String, Object> weekOnline: exitList) {
            mapExitList.add(Convert.toInt(weekOnline.get("exit_num"), 0));
        }
        List<Map<String, Object>> bookingBusinessList = qmsMainBusinessService.selectBookingBusinessCount(qmsMainBusiness);
        List<Map<String, Object>> mapbookingBusinessList = new ArrayList<>();
        for (Map<String, Object> weekOnline: bookingBusinessList) {
            Map<String, Object> tmpMap = new HashMap<>();
            tmpMap.put("businessName", weekOnline.get("erp_business_name"));
            tmpMap.put("num", weekOnline.get("num"));
            mapbookingBusinessList.add(tmpMap);
        }
        map.put("xDate", xDateList);
        map.put("bookingList", mapBookingList);
        map.put("takeNoList", mapTakeNoList);
        map.put("entryList", mapEntryList);
        map.put("exitList", mapExitList);
        map.put("bookingBusinessList", mapbookingBusinessList);
        return AjaxResult.success(map);
    }

}
